package com.doctcloud.datacloud.controller;

import com.doctcloud.common.core.web.domain.AjaxResult;
import com.doctcloud.datacloud.service.*;
import com.doctcloud.system.api.domain.SysDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import sun.security.mscapi.CPublicKey;


import java.util.Date;

import static com.doctcloud.common.core.web.domain.AjaxResult.success;

@RestController
@RequestMapping("/api/")
public class DataController {
    @Autowired
    private DeptDictEntityService deptDictEntityService;
    @Autowired
    private PriceListEntityService priceListEntityService;
    @Autowired
    private PatMasterIndexEntityService patMasterIndexEntityService;
    @Autowired
    private InpBillDetailEntityService inpBillDetailEntityService;
    @Autowired
    private OrdersCostsEntityService ordersCostsEntityService;
    @Autowired
    private OrdersEntityService ordersEntityService;
    @Autowired
    private TransferEntityService transferEntityService;
    @Autowired
    private OutpRcptMasterEntityService outpRcptMasterEntityService;
    @Autowired
    private OutpOrderDescEntityService outpOrderDescEntityService;
    @Autowired
    private OutpBillItemsEntityService outpBillItemsEntityService;
    @Autowired
    private PatVisitEntityService patVisitEntityService;
    @GetMapping("syncdept")
    public AjaxResult list() {
        deptDictEntityService.syncDeptDict();
        return success();
    }
    @GetMapping("listtest")
    public AjaxResult listdept() {
        return success(deptDictEntityService.list());
    }
    @GetMapping("syncpricelist")
    public AjaxResult pricelistSync(){
        priceListEntityService.syncPriceList();
        return success();
    }
    @GetMapping("syncpatsinhospital")
    public AjaxResult patvisitSync(){
        patMasterIndexEntityService.syncPatsInHospital();
        return success();
    }

    @GetMapping("syncinpbillbypat")
    public AjaxResult inpillSync(@RequestParam("patientId") String patientId,
                                 @RequestParam("visitId") Integer visitId){
        try {
            // 调用服务层方法执行同步逻辑
            inpBillDetailEntityService.syncPat(patientId, visitId);
            // 如果成功，返回成功信息
            return AjaxResult.success("同步成功");
        } catch (Exception e) {
            // 如果发生异常，捕获并记录日志
//            log.error("同步患者[{}]的住院账单时发生异常: ", patientId, e);
            // 返回包含错误信息的结果
            return AjaxResult.error("同步失败：" + e.getMessage());
        }
    }
    @GetMapping("syncorderscosts")
    public AjaxResult orderscostsSync(@RequestParam("patientId") String patientId,
                                      @RequestParam("visitId") Integer visitId){
        try {
            // 调用服务层方法执行同步逻辑
            ordersCostsEntityService.syncPat(patientId, visitId);
            // 如果成功，返回成功信息
            return AjaxResult.success("同步成功");
        } catch (Exception e) {
            // 如果发生异常，捕获并记录日志
//            log.error("同步患者[{}]的住院账单时发生异常: ", patientId, e);
            // 返回包含错误信息的结果
            return AjaxResult.error("同步失败：" + e.getMessage());
        }
    }

    @GetMapping("syncorders")
    public AjaxResult ordersSync(@RequestParam("patientId") String patientId,
                                      @RequestParam("visitId") Integer visitId){
        try {
            // 调用服务层方法执行同步逻辑
            ordersEntityService.syncPat(patientId, visitId);
            // 如果成功，返回成功信息
            return AjaxResult.success("同步成功");
        } catch (Exception e) {
            // 如果发生异常，捕获并记录日志
//            log.error("同步患者[{}]的住院账单时发生异常: ", patientId, e);
            // 返回包含错误信息的结果
            return AjaxResult.error("同步失败：" + e.getMessage());
        }
    }
    @GetMapping("synctransfer")
    public AjaxResult transferSync(@RequestParam("patientId") String patientId,
                                 @RequestParam("visitId") Integer visitId){
        try {
            // 调用服务层方法执行同步逻辑
            transferEntityService.syncPat(patientId, visitId);
            // 如果成功，返回成功信息
            return AjaxResult.success("同步成功");
        } catch (Exception e) {
            // 如果发生异常，捕获并记录日志
//            log.error("同步患者[{}]的住院账单时发生异常: ", patientId, e);
            // 返回包含错误信息的结果
            return AjaxResult.error("同步失败：" + e.getMessage());
        }
    }
    @GetMapping("syncrcptmaster")
    public AjaxResult outprcptSync(@RequestParam("startDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                                   @RequestParam("endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate){
        try {
            outpRcptMasterEntityService.syncRcptMaster(startDate, endDate);
            return AjaxResult.success("同步成功");
        } catch (Exception e) {
            return AjaxResult.error("同步失败：" + e.getMessage());
        }
    }
    @GetMapping("syncoutporder")
    public AjaxResult outporderSync(@RequestParam("rcptNo") String rcptNo){
        try {
            outpOrderDescEntityService.syncRcpt(rcptNo);
            return AjaxResult.success("同步成功");
        } catch (Exception e) {
            return AjaxResult.error("同步失败：" + e.getMessage());
        }
    }

    @GetMapping("syncoutpbill")
    public AjaxResult outpbillSync(@RequestParam("rcptNo") String rcptNo){
        try {
            outpBillItemsEntityService.syncRcpt(rcptNo);
            return AjaxResult.success("同步成功");
        } catch (Exception e) {
            return AjaxResult.error("同步失败：" + e.getMessage());
        }
    }

    @GetMapping("syncpatvisitbyadmin")
    public AjaxResult patVisitAdmSync(@RequestParam("statDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date statDate){
        try {
            patVisitEntityService.syncAdminAfterDate(statDate);
            return AjaxResult.success("同步成功");
        } catch (Exception e) {
            return AjaxResult.error("同步失败：" + e.getMessage());
        }
    }
    @GetMapping("syncpatvisitbydischarge")
    public AjaxResult patVisitDisSync(@RequestParam("statDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date statDate){
        try {
            patVisitEntityService.syncDischargeAfterDate(statDate);
            return AjaxResult.success("同步成功");
        } catch (Exception e) {
            return AjaxResult.error("同步失败：" + e.getMessage());
        }
    }
}